<link rel="import" href="../bower_components/app-route/app-route.html">
<link rel="import" href="../bower_components/mist-list/mist-list.html">
<link rel="import" href="../bower_components/paper-fab/paper-fab.html">

<link rel="import" href="tunnels/tunnel-add.html">
<link rel="import" href="tunnels/tunnel-actions.html">
<link rel="import" href="tunnels/tunnel-page.html">
<link rel="import" href="helpers/mist-lists-behavior.html">
<link rel="import" href="helpers/owner-filter-behavior.html">


<dom-module id="page-tunnels">
    <template>
        <style include="shared-styles">
            [hidden] {
                display: none !important;
            }
        </style>
        <app-route route="{{route}}" pattern="/:tunnel" data="{{data}}"></app-route>
        <template is="dom-if" if="[[_isListActive(route.path)]]" restamp>
            <tunnel-actions id="actions" items="[[selectedItems]]" actions="{{actions}}" user="[[model.user.id]]" members="[[model.membersArray]]" org="[[model.org]]">
                <mist-list selectable resizable column-menu multi-sort
                    id="tunnelsList"
                    apiurl="/api/v1/tunnels"
                    item-map="[[model.tunnels]]"
                    name="Tunnels"
                    selected-items="{{selectedItems}}"
                    filtered-items-length="{{filteredItemsLength}}"
                    frozen=[[_getFrozenLogColumn()]]
                    visible=[[_getVisibleColumns()]]
                    renderers=[[_getRenderers(model.tunnels)]]
                    route={{route}}
                    user-filter=[[model.sections.tunnels.q]]
                    primary-field-name="id"
                    filter-method="[[_ownerFilter()]]"
                    actions="[[actions]]">
                    <p slot="no-items-found">No tunnels found.</p>
                </mist-list>
            </tunnel-actions>
            <div class="absolute-bottom-right" hidden$="[[!check_perm('add','tunnel', null, model.org, model.user)]]">
                <paper-fab id="tunnelAdd" icon="add" on-tap="_addResource"></paper-fab>
            </div>
        </template>
        <tunnel-add model="[[model]]" section="[[model.sections.tunnels]]" hidden$=[[!_isAddPageActive(route.path)]]></tunnel-add>
        <tunnel-page model="[[model]]" tunnel$="[[_getTunnel(data.tunnel, model.tunnels, model.tunnels.*)]]" resource-id="[[data.tunnel]]" section="[[model.sections.tunnels]]" hidden$=[[!_isDetailsPageActive(route.path)]]>
            </key-page>
    </template>
    <script>
    Polymer({
        is: 'page-tunnels',
        behaviors: [
            mistListsBehavior,
            ownerFilterBehavior,
            rbacBehavior
        ],

        properties: {
            model: {
                type: Object
            },
            ownership: {
                type: Boolean
            },
            actions: {
                type: Array,
                notify: true
            },
            selectedItems: {
                type: Array,
                notify: true
            }
        },

        listeners: {
            'select-action': 'selectAction',
        },
        _isAddPageActive: function(path) {
            return path == '/+add';
        },
        _isDetailsPageActive: function(path) {
            if (path && path != '/+add' && this.shadowRoot && this.shadowRoot.querySelector('tunnel-page'))
                this.shadowRoot.querySelector('tunnel-page').updateState();
            return path && path != '/+add';
        },
        _isListActive: function(path) {
            return !path;
        },
        _getTunnel: function(id) {
            if (this.model.tunnels)
                return this.model.tunnels[id];
        },
        _addResource: function(e) {
            this.dispatchEvent(new CustomEvent('go-to', { bubbles: true, composed: true, detail: {
                url: this.model.sections.tunnels.add
            } }));
        },
        _getFrozenLogColumn: function() {
            return ['name'];
        },

        _getVisibleColumns: function() {
            var ret = ['cidrs', 'created_by', 'id', 'tags'];
            if (this.model.org && this.model.org.ownership_enabled == true)
                ret.push('owned_by');
            return ret;
        },
        _getRenderers: function(tunnels) {
            var _this = this;
            return {
                'name': {
                    'body': function(item, row) {
                        return '<strong class="name">' + item + '</strong>';
                    }
                },
                'cidrs': {
                    'body': function(item, row) {
                        if (item)
                            return item.join(', ');
                    }
                },
                'owned_by': {
                    'title': function(item, row) {
                        return 'owner';
                    },
                    'body': function(item, row) {
                        return _this.model.members[item] ? _this.model.members[item].name || _this.model.members[item].email || _this.model.members[item].username : '';
                    }
                },
                'created_by': {
                    'title': function(item, row) {
                        return 'created by';
                    },
                    'body': function(item, row) {
                        return _this.model.members[item] ? _this.model.members[item].name || _this.model.members[item].email || _this.model.members[item].username : '';
                    }
                },
                'tags': {
                    'body': function(item, row) {
                        var tags = item,
                            display = "";
                        for (key in tags) {
                            display += "<span class='tag'>" + key;
                            if (tags[key] != undefined && tags[key] != "")
                                display += "=" + tags[key];
                            display += "</span>";
                        }
                        return display;
                    }
                }
            }
        },

        selectAction: function(e) {
            e.stopImmediatePropagation();
            if (this.shadowRoot.querySelector('#tunnelsList')) {
                this.shadowRoot.querySelector('#tunnelsList').shadowRoot.querySelector('#actions').selectAction(e);
            }
        }
    });
    </script>
</dom-module>